/*
 * 项目名称:keel-flower
 * 类名称:DynamicRequiresPermissionsAspect.java
 * 包名称:me.keelflower.aspect
 *
 * 修改履历:
 *      日期                修正者      主要内容
 *      2021/3/3 17:12    liliudong      初版完成
 *
 */
package com.liliudong.keelflower.upms.admin.aspect;

import cn.hutool.core.util.StrUtil;
import com.liliudong.keelflower.common.core.annotation.DynamicRequiresPermissions;
import com.liliudong.keelflower.common.core.constants.HttpStatus;
import com.liliudong.keelflower.common.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * 动态权限切面类
 *
 * @author liliudong 2021/3/3 17:12
 */
@Slf4j
@Aspect
@Component
public class RequiresPermissionsAspect {

    @Pointcut("@annotation(com.liliudong.keelflower.common.core.annotation.DynamicRequiresPermissions)")
    public void dynamicRequiresPermissions() {

    }

    @Before("dynamicRequiresPermissions()")
    public void before(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getName();
        String permissionPrefix = getPermissionPrefix(className);
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        DynamicRequiresPermissions annotation = signature.getMethod().getAnnotation(DynamicRequiresPermissions.class);
        if (annotation != null) {
            ArrayList<String> permissionArr = new ArrayList<>();
            String[] value = annotation.value();
            for (String permission : value) {
                permissionArr.add(permissionPrefix.concat(permission));
            }
//            SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
//            Set<String> stringPermissions = user.getStringPermissions();
//            if (!stringPermissions.containsAll(permissionArr)) {
//                throw new UnauthorizedException("需要权限:[" + String.join(",", permissionArr) + "]");
//            }

        }
    }

    /**
     * 获取权限前缀
     *
     * @param allClassName
     * @return
     */
    private String getPermissionPrefix(String allClassName) {
        String className = StrUtil.removeSuffix(allClassName.substring(allClassName.lastIndexOf(".") + 1), "Controller");
        StringBuilder buffer = new StringBuilder();
        if (StrUtil.isNotBlank(className)) {
            for (int i = 0; i < className.length(); i++) {
                char c = className.charAt(i);
                if (Character.isUpperCase(c)) {
                    if (i != 0) {
                        buffer.append(":");
                    }
                    buffer.append(String.valueOf(c).toLowerCase());
                } else {
                    buffer.append(c);
                }
            }
            buffer.append(":");
            return buffer.toString();
        }
        log.error("类名捕获失败");
        throw new BusinessException(HttpStatus.SYSTEM_BUSY);
    }

}
